using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Principal;
using System.Security;
using System.Web.Security;

namespace Microsoft.ConnectionEngine.Common.Security
{
    /// <summary>
    /// HSD Principal class using IPrincipal Interface
    /// </summary>
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://Microsoft.ConnectionEngine/Schemas/ServiceProviderRegister")]
    public class HSDPrincipal: IPrincipal
    {
        HSDIdentity _identity;
        /// <summary>
        /// Storngly Typed Identity Object Associated with the Principal
        /// </summary>
        public HSDIdentity HSDIdentity
        {
            get { return _identity; }
            set { _identity = value; }
        }

        //cannot serialize an interface        
        /// <summary>
        /// Generic Identity Object 
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public IIdentity Identity
        {
            get { return (HSDIdentity)_identity; }
            set { _identity = (HSDIdentity)value; }
        }
        
        private System.Collections.Generic.List<String> _roles;
        /// <summary>
        /// List of Roles for the Principal
        /// </summary>
        [System.Xml.Serialization.XmlArrayItem(typeof(string), ElementName = "Role")]
        public System.Collections.Generic.List<String> Roles
        {
            get
            {
                if (_roles == null) _roles = new List<String>();
                return _roles;
            }
            set { _roles = value; }
        }
        /// <summary>
        /// Checking Method to Role assignement
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool IsInRole(string role)
        {
            return Roles.Contains(role);                        
        }
        /// <summary>
        /// Default Constructor
        /// </summary>
        public  HSDPrincipal()            
        {
        }
        /// <summary>
        /// Parameterised Constructor
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="roles"></param>
        public HSDPrincipal(HSDIdentity identity, List<String> roles)            
        {
            _identity = identity;
            _roles = roles;
        }                                
    }
}
